class SplitBox extends eui.Rect {
	_minX = 300;

	_maxX = 600;

	_dragX: boolean;

	_dragY: boolean;
}

const BOXX = 300;
const BOXW = 10;

function getRightWindow(){
	return MainFrame.ins().rightWin;
}

class MainFrame extends DockWindow {
	private static instance: MainFrame;

	public static ins(): MainFrame {
		if (!this.instance) {
			this.instance = new MainFrame();
		}
		return this.instance;
	}

	constructor() {
		super();
		this.skinName = 'skin_main';
		fullAlign(this);
	}

	leftWin: LeftArea;

	rightWin: RightArea;

	box: SplitBox;

	override childrenCreated(): void {
		super.childrenCreated();
		this.width = this.parent.width;

		this.updateRightWidth();

		this.box.addEventListener(MainDrag.dragEvent, this.onSplitBoxDrag, this);
		MainWindow.ins().bindDrag(this.box, false);
		this.box._dragY = false;

		this.rightWin.aniWin.bindTree(this.leftWin.ani);
		this.rightWin.charWin.bindTree(this.leftWin.char);

		// this.leftWin.selectTab(TREEID.SCENE, true);
		this.leftWin.drama.bindTree(this.rightWin.dramaWin.tree);
		this.leftWin.script.bindTree(this.rightWin.scriptWin.tree);
		this.leftWin.story.bindTree(this.rightWin.storyWin.tree);
		this.leftWin.action.bindTree(this.rightWin.actionWin.tree);
		// this.leftWin.magic.bindTree(this.rightWin.magicWin.tree);

		this.addEventListener(egret.Event.RESIZE, this.onResize, this);
	}

	onResize(): void {
		ui.msgDebug('MainFrame.onResize', this.width, this.height);
		this.updateRightWidth();
	}

	onSplitBoxDrag(e: egret.Event): void {
		// console.log("onSplitBox",e.data)

		this.leftWin.width = e.data.x;
		this.rightWin.x = e.data.x + BOXW;
		// this.rightWin.setWidth(this.width - this.rightWin.x)
		this.updateRightWidth();
	}

	updateRightWidth(): void {
		if (this.isFull()) {
			this.rightWin.width = this.width;
		} else {
			this.rightWin.width = this.width - (this.leftWin.width + this.box.width);
		}
		// game.GameMap.ins().setWinSize(this.rightWin.width, this.height);
		// console.log('onSplitBox', this.leftWin.width, this.rightWin.width);
	}

	// openBuilding(file: string): void {
	// 	this.rightWin.buildWin.openBuilding(file);
	// }


	group: eui.Group;

	toggleFull(): void {
		// this.rightWin.x = 0;
		// ui.msgline('toggle_full');
		if (!this.isFull()) {
			this.group.removeChild(this.leftWin);
			this.addChild(this.leftWin);
			this.leftWin.visible = false;
			this.group.removeChild(this.box);
			this.rightWin.width = this.width;
		} else {
			this.removeChild(this.leftWin);
			this.group.addChild(this.leftWin);
			this.group.addChild(this.box);
			this.leftWin.visible = true;
		}
		this.updateRightWidth();
	}

	isFull(): boolean {
		return !this.leftWin.visible;
	}

	onTab(shift: boolean): void {
		if (this.isFull()) {
			this.rightWin.onTab(shift);
		} else {
			const { focus } = MainWindow.ins().focus;
			const win = isOldParent(focus, this.rightWin) ? this.rightWin : this.leftWin;
			win.onTab(shift);
		}
	}
}
